﻿using BarcodeDelay.bean;
using BarcodeDelay.FrameDecoder;
using BarcodeDelay.logHelp;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace BarcodeDelay.business
{
    public class LeaveMessageHelperGen
    {
        private StreamWriter swb = null;
        FileStream bFile = null;
        ArrayList m_arListDepBarcodes = new ArrayList();
        Hashtable htCurMinutes = new Hashtable();
        public event Action AddMessage;
        public event Action GotDelayBarcode;
        public event Action GotFailedBarcode;

        public bool isLastLeaveIsBig = false;

        

        public event Action QueryMessageRes;
        public event Action DoneHttpResponse;
        private static LeaveMessageHelperGen instance;
        public static LeaveMessageHelperGen Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new LeaveMessageHelperGen();
                }
                return instance;
            }
        }
        private bool isManual = false;
        private Thread fThreadDep = null;
        public void BeginAnalyThread(ArrayList arListBarCodes, string anayType)
        {
            try
            {
                initLog();
                m_arListDepBarcodes.Clear();
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    fThreadDep.Abort();
                    System.Threading.Thread.Sleep(50);
                }
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    while (fThreadDep.ThreadState != ThreadState.Aborted)
                    {
                        //当调用Abort方法后，如果thread线程的状态不为Aborted，主线程就一直在这里做循环，直到thread线程的状态变为Aborted为止
                        Thread.Sleep(100);
                    }
                }


                m_arListDepBarcodes = arListBarCodes;
                if (anayType.Contains("manual"))
                {
                    isManual = true;
                }
                if (anayType.Contains("leaveMsgBoard"))
                {                    
                    fThreadDep = new Thread(new ParameterizedThreadStart(parseLeaveMsgBoard));
                    fThreadDep.Start((object)arListBarCodes);

                    //fThreadDep = new Thread(new ThreadStart(parseLeaveMsgBoard));//出港分析 开辟一个新的线程
                }
                else
                {
                    //fThreadDep = new Thread(new ThreadStart(parseDepartureBarcodes));//出港分析 开辟一个新的线程
                    //fThreadDep.Start();
                    fThreadDep = new Thread(new ParameterizedThreadStart(parseCuijianBarcodes));
                    fThreadDep.Start((object)arListBarCodes);
                }
                
                fThreadDep.IsBackground = true;
              

            }
            catch (Exception ex)
            {

            }
        }



        public void parseLeaveMsgBoard(object arListDepBarcodes)
        {
            //StoParams.Instance.htResWechat.Clear();
            string resMsg = "";
            int totalDelays = 0;
            int queryServerResTime = 0;
            int priorBarCodeCount = 0;
            bool lastAddSuccess = false;
            ArrayList arListDepResultTmp = new ArrayList();
            try
            {
                ArrayList arListTmpC = (ArrayList)arListDepBarcodes;
                logHelp.LogHelper.Instance.writeLog(" 开始留言。。。个数 " + arListTmpC.Count.ToString());
                if (arListTmpC.Count > 10)
                {
                    isLastLeaveIsBig = true;
                }
                else
                {
                    isLastLeaveIsBig = false;
                }
                if (isLastLeaveIsBig)
                {
                    if(!StoParams.Instance.register_site.Contains("永平"))
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                    
                }
                int totalQueryCount = 0;
                ArrayList arListBarcodes = new ArrayList();
                ArrayList arListTmp = new ArrayList();
                RoadInfo roadInfoGen = new bean.RoadInfo();
                Hashtable htRoadMap = new Hashtable();
                bool isNewStart = false;
                string barcode = "";
                ArrayList arListBarCodes = new ArrayList();
                arListBarCodes.Clear();
                DateTime dtStart = System.DateTime.Now;
                DelayAnaHelperGen.Instance.m_arlist_Delayed_BarCodes.Clear();


                if (arListTmp.Count > 0)
                {
                    arListBarcodes.Add(arListTmp.Clone());
                    arListTmp.Clear();
                }
                int sumQueryTime = 0;
                int avgQueryTime = 2;
                int intProcess = 0;


                string httpRes = "";
                ArrayList arListAllGXTask = new ArrayList();
                int curCount = 0;
                ArrayList arListTmp3 = (ArrayList)arListDepBarcodes;
                StoParams.Instance.totalParcels = arListTmp3.Count;
                foreach (ParcelBean parcelBean in (ArrayList)arListDepBarcodes)
                {
                    StoParams.Instance.curParces++;
                    if (StoParams.Instance.totalParcels > 20)
                    {
                        if(lastAddSuccess)
                        {
                            if (!StoParams.Instance.register_site.Contains("永平"))
                            {
                                System.Threading.Thread.Sleep(1200);
                            }
                                
                        }
                        
                    }
                    if (curCount > 0 && curCount % 10 == 0)
                    {
                        System.Threading.Thread.Sleep(1500);
                    }
                    lastAddSuccess = false;
                    curCount++;
                    string strBarCode = parcelBean.wayBill;
                    StoParams.Instance.currentBarcode = strBarCode;
                    string pickName = "";
                    string orderSource = "";
              
                    string curSiteToken = "";
                    string customerName = "";
                    if (!UtilityHelp.Instance.resetToken(strBarCode, out pickName, out orderSource, out curSiteToken,out customerName))
                    {
                       
                        if (curSiteToken == "")
                        {
                            FrameDecoderManager.Instance.htFailedParcel[strBarCode] = System.DateTime.Now;
                            StoParams.Instance.htResWechat[strBarCode] = " 发件网点 " + pickName + " 登录失效";
                            if (pickName != "" && StoParams.Instance.isStrictMode)
                            {
                                if (StoParams.Instance.register_site != pickName)
                                {
                                    StoParams.Instance.htResWechat[strBarCode] = "发件网点" + pickName + "  非我司单号  无法处理";
                                }
                            }
                            continue;
                        }
                        StoParams.Instance.htResWechat[strBarCode] = " 发件网点 " + pickName + " 非我司单号  无法处理";

                        if (pickName != "" && StoParams.Instance.isStrictMode)
                        {
                            if (StoParams.Instance.register_site != pickName)
                            {
                                StoParams.Instance.htResWechat[strBarCode] = "发件网点" + pickName + "  非我司单号  无法处理";
                            }
                        }
                        continue;
    
                    }
                    if (pickName.Contains("授权"))
                    {
                        continue;
                    }
                    parcelBean.lanshouSite = pickName;
                    parcelBean.customerName = customerName;
                    UtilityHelp.Instance.resetWechat(parcelBean, pickName);
                    string wechatName = "";
                    StoParams.Instance.resMsg = "";
                    intProcess++;
                    string strLeftTime = "10";

                    bool isReturnInRepley = false;
                    ArrayList arListRoadMap = JTDelayHelper.Instance.getRoadMap(strBarCode, out httpRes, arListAllGXTask);
                    ArrayList arListDBOrJB = new ArrayList();
                    string lanshouTime = JTDelayHelper.Instance.getLanShouTime(arListRoadMap);
                    parcelBean.lanshouTime = lanshouTime;

                    if (!httpRes.Contains("成功"))
                    {
                        StoParams.Instance.resMsg = httpRes;
                        QueryMessageRes();
                        if (isManual)
                        {
                            System.Threading.Thread.Sleep(200);
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(150);
                        }

                        resMsg = "请求失败";
                        StoParams.Instance.htResWechat[strBarCode] = "请求失败";
                        continue;
                    }
                    if (arListRoadMap.Count == 0 && httpRes.Contains("成功"))
                    {
                        /*if (isManual)
                        {
                            System.Threading.Thread.Sleep(150);
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(150);
                        }*/

                        StoParams.Instance.htResWechat[strBarCode] = "快件未发出";
                        continue;
                    }
                    if (JTDelayHelper.Instance.isOnlySelfSiteMap(arListRoadMap))
                    {
                        /*if (isManual)
                        {
                            System.Threading.Thread.Sleep(300);
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(300);
                        }*/
                        StoParams.Instance.htResWechat[strBarCode] = "只有本网点路由";
                        continue;
                    }
                    string parcelType = "";
                    string lastParCelStatus = UtilityHelp.Instance.getLastParCelStatus(arListRoadMap);

                    string overTimeMention = UtilityHelp.Instance.getOverTimeParcel(arListRoadMap);
                    if (UtilityHelp.Instance.isProblemParcel(arListRoadMap, pickName,out parcelType))
                    {
                        StoParams.Instance.resMsg = " 以上单号为 "+ parcelType + "，不再催件";
                        StoParams.Instance.htResWechat[strBarCode] = "以上单号为 "+ parcelType + "，不再催件";
                       
                        continue;
                    }
                    if (JTDelayHelper.Instance.isNoSiteToLeaveMsg(arListRoadMap, pickName))
                    {
                        System.Threading.Thread.Sleep(300);
                        StoParams.Instance.htResWechat[strBarCode] = "快件未到转运";
                        continue;
                    }
                    bool isYiqingDelay = false;
                    /*if (JTDelayHelper.Instance.isYiqingStay(arListRoadMap))
                    {
                        isYiqingDelay = true;
                        StoParams.Instance.htResWechat[strBarCode] = "此件因疫情时效不保，\r\n收件人着急建议先给收件人退款，\r\n退款后通知我司拦截 ";
                        if (StoParams.Instance.register_site.Contains("后宅"))
                        {
                            if (isManual)
                            {
                                System.Threading.Thread.Sleep(300);
                            }
                            else
                            {
                                System.Threading.Thread.Sleep(300);
                            }
                            continue;
                        }
                  
                    }*/
                    string returnMySite = "";
                    if (JTDelayHelper.Instance.isLastSiteIsMyself(arListRoadMap, out returnMySite))
                    {
                        StoParams.Instance.htResWechat[strBarCode] = returnMySite;
                        System.Threading.Thread.Sleep(100);
                        continue;
                    }
                    string commentType = "";
                    string strInterCeptSiteCode = "";



                    string siteName = "";
                    string leaveMsg = "";
                    bool hasSleep = false;
                    JTDelayHelper.Instance.parseManualBarcode(arListRoadMap, out commentType, out strInterCeptSiteCode, out siteName, out leaveMsg, parcelBean.isYishi,true);
                    bool isLeaveSuccess = false;
                    if (strInterCeptSiteCode != "")
                    {
                        if (parcelBean.ori_addr.Replace(" ", "").Contains("改电话") || parcelBean.ori_addr.Replace(" ", "").Contains("改收件人电话")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("改收货人电话") || parcelBean.ori_addr.Replace(" ", "").Contains("改联系电话")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("改联系人电话")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("电话修改为")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("电话改为")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("新电话号码")
                            
                            || parcelBean.ori_addr.Replace(" ", "").Contains("电话换成")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("电话换为")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("号码改为")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("电话变为")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("手机修改为")
                             || parcelBean.ori_addr.Replace(" ", "").Contains("号码修改为")
                            || UtilityHelp.Instance.isBingoMainTxt(parcelBean.ori_addr, "改", "电话", 5)
                            || UtilityHelp.Instance.isBingoMainTxt(parcelBean.ori_addr, "改", "号码", 5))
                        {
                            commentType = "改电话";
                            parcelBean.replyType = "改电话";
                            leaveMsg = parcelBean.ori_addr;
                        }
                        else if (parcelBean.ori_addr.Length > 1 && (parcelBean.ori_addr.Replace(" ", "").Contains("短少") 
                            || parcelBean.ori_addr.Replace(" ", "").Contains("破损")
                             || parcelBean.ori_addr.Replace(" ", "").Contains("少件")
                              || parcelBean.ori_addr.Replace(" ", "").Contains("空包")
                            || parcelBean.ori_addr.Replace(" ", "").Contains("污染")))
                        {
                            commentType = "破损";
                            parcelBean.replyType = "破损";
                            //res = "此件破损麻烦提供外包装破损照片内物破损照片，\r\n我司联系派件网点核实情况";
                        }
                        string errorMsg = "";

                        string minuteDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                        int totalMinutes = 0;
                        if (!htCurMinutes.ContainsKey(minuteDate))
                        {
                            htCurMinutes[minuteDate] = 1;
                        }
                        else
                        {
                            totalMinutes = Convert.ToInt32(htCurMinutes[minuteDate]) + 1;
                            htCurMinutes[minuteDate] = totalMinutes;
                        }
                        if (totalMinutes > 10)
                        {
                            if(!StoParams.Instance.register_site.Contains("永平"))
                            {
                                System.Threading.Thread.Sleep(1000);
                            }
                            

                        }

                        /*if (!JTDelayHelper.Instance.isParcelLeaveFirstCenter(arListRoadMap, pickName))
                        {
                            parcelBean.replyType = "催件未出集散或转运";
                            StoParams.Instance.htResWechat[strBarCode] = "快件未出" + "集散/转运" + "暂时无法留言";
                            continue;
                        }*/


                        if (parcelBean.taskType == "延误查件")
                        {
                            commentType = "延误查件";
                        }

                        if (commentType.Contains("签收"))
                        {
                            if (!StoParams.Instance.register_site.Contains("廉江"))
                            {
                                parcelBean.isYishi = true;
                            }
                        }

                        if(!parcelBean.isYishi)
                        {
                            UtilityHelp.Instance.parseIsYishi(parcelBean, arListRoadMap);
                        }
                        if(StoParams.Instance.register_site.Contains("永平")&& !commentType.Contains("破损") && !commentType.Contains("改电话"))
                        {
                            parcelBean.isYishi = true;
                        }


                        if (commentType.Contains("签收")&& parcelBean.ori_addr.Length<6&&StoParams.Instance.register_site.Contains("茂名"))
                        {
                            StoParams.Instance.htResWechat[strBarCode] = "以上单号已签收";
                            continue;
                        }


                       

                        if (isManual)
                        {
                            parcelBean.wechat = StoParams.Instance.register_wechat;
                            if (commentType.Contains("签收") && parcelBean.ori_addr.Length < 6)
                            {
                                StoParams.Instance.htResWechat[strBarCode] = "以上单号已签收";
                                if(overTimeMention!="")
                                {
                                    StoParams.Instance.htResWechat[strBarCode] = overTimeMention;
                                }
                                continue;
                            }
                        }

                        FrameDecoderManager.Instance.lastSendRes = System.DateTime.Now;
                        string fullMsg = "";
                        string phoneNumber = "";
                        if(parcelBean.phoneNumList.Count>0)
                        {
                            phoneNumber = parcelBean.phoneNumList[parcelBean.phoneNumList.Count-1];
                            
                        }

                        
                        if (addComment(phoneNumber,strBarCode, commentType, parcelBean.ori_addr, leaveMsg, parcelBean.wechat, out errorMsg, out fullMsg, parcelBean.isYishi, parcelBean.roomID))
                        //if (UtilityHelp.Instance.addCommentReg(strBarCode, commentType, false))
                        {
                            if (StoParams.Instance.register_site.Contains("禅城") && !commentType.Contains("破损") && !commentType.Contains("改电话"))
                            {

                                if(phoneNumber!="")
                                {
                                    parcelBean.isYishi = false;
                                    addComment(phoneNumber, strBarCode, commentType, parcelBean.ori_addr, leaveMsg, parcelBean.wechat, out errorMsg, out fullMsg, parcelBean.isYishi, parcelBean.roomID);
                                }
                                   
                            }
                            lastAddSuccess = true;
                            StoParams.Instance.addCommentParcels++;
                            StoParams.Instance.curDelayBarcode = strBarCode;
                            StoParams.Instance.resMsg = "留言成功";
                            if (isManual)
                            {
                                if(GotDelayBarcode!=null)
                                {
                                    GotDelayBarcode();
                                }
                                
                            }

                            StoParams.Instance.currentBarcode = strBarCode;
                            StoParams.Instance.resMsg = " 以上单号已联系网点催促";
                            if (isManual)
                            {
                                if(AddMessage!=null)
                                {
                                    AddMessage.Invoke();
                                }
                                
                            }

                            //if(StoParams.Instance.register_site.Contains("湛江"))
                            {
                                if (!commentType.Contains("签收") && fullMsg.Length > 10)
                                {
                                    fullMsg = UtilityHelp.Instance.getUsefullContent(fullMsg, siteName);
                                    hasSleep = true;
                                    Thread.Sleep(200);

                              
                                    //if (StoParams.Instance.register_site.Contains("茂名"))
                                    {
                                        fullMsg = fullMsg.Replace("，", ",");
                                        fullMsg = fullMsg.Replace("延误仲裁查件", "");
                                        fullMsg = fullMsg.Replace("遗失仲裁查件", "");
                                        fullMsg = fullMsg.Replace("破损仲裁查件", "");
                                        fullMsg = fullMsg.Trim().TrimStart(',');

                                    }
                                    

                                    signProblemParcel(strBarCode, out errorMsg, strInterCeptSiteCode, siteName, fullMsg);
                                }
                            }

                            isLeaveSuccess = true;
                        }
                        else
                        {
                            StoParams.Instance.curFailedBarcode = strBarCode;
                            StoParams.Instance.resMsg = "留言失败";
                            if (isManual)
                            {
                                if(GotFailedBarcode!=null)
                                {
                                    GotFailedBarcode();
                                }
                                
                            }


                            StoParams.Instance.curFailedBarcode = strBarCode;
                            StoParams.Instance.resMsg = " 以上单号催促失败" + errorMsg;
                        }
                    }
                    resMsg = StoParams.Instance.resMsg;
                    string reswechatMsg = UtilityHelp.Instance.getLeaveMsgResponse(arListRoadMap, commentType, parcelBean, isYiqingDelay);
                    if (!StoParams.Instance.resMsg.Contains("失败"))
                    {
                        StoParams.Instance.htResWechat[strBarCode] = reswechatMsg;

                       
                        if (commentType.Contains("签收"))
                        {
                            if (StoParams.Instance.register_site.Contains("锡山"))
                            {
                                //StoParams.Instance.htResWechat[strBarCode] = "签收没有收到，提供客户信息，我司通知核实";
                            }
                            else
                            {
                                //StoParams.Instance.htResWechat[strBarCode] = "签收没有收到，麻烦提供签收底单，我司通知核实";
                            }
                            
                            //continue;
                        }

                        
                    }
                    else
                    {
                        StoParams.Instance.htResWechat[strBarCode] = resMsg;
                        


                    }

                    if(overTimeMention.Length>3)
                    {
                        StoParams.Instance.htResWechat[strBarCode] = overTimeMention;
                    }
                   
                    StoParams.Instance.resMsg = strBarCode + " " + StoParams.Instance.resMsg;
                    if (!isLeaveSuccess)
                    {
                        if(QueryMessageRes!=null)
                        {
                            QueryMessageRes.Invoke();
                        }
                        
                    }

                    Random ran = new Random();
                    if(DoneHttpResponse!=null)
                    {
                        DoneHttpResponse();
                    }
                    
                    int sleepTime = ran.Next(150, 300);
                    if (StoParams.Instance.totalParcels < 20)
                    {
                        if (curCount > 8)
                        {
                            sleepTime = ran.Next(300, 600);
                            System.Threading.Thread.Sleep(sleepTime);
                        }
                        else
                        {
                            if (!hasSleep)
                            {
                                Thread.Sleep(sleepTime);
                            }

                        }
                    }


                    StoParams.Instance.currentBarcode = strBarCode.Trim();




                }



            }
            catch (Exception ex)
            {


            }
            int tiot = queryServerResTime;



        }

        public void parseGuifanInterNotReturnMsgBoard(object arListDepBarcodes)
        {
            //StoParams.Instance.htResWechat.Clear();
            string resMsg = "";
            int totalDelays = 0;
            int queryServerResTime = 0;
            int priorBarCodeCount = 0;
            ArrayList arListDepResultTmp = new ArrayList();
            try
            {
                ArrayList arListTmpC = (ArrayList)arListDepBarcodes;
                logHelp.LogHelper.Instance.writeLog(" 开始留言。。。个数 "+ arListTmpC.Count.ToString());
                if(arListTmpC.Count>10)
                {
                    isLastLeaveIsBig = true;
                }
                else
                {
                    isLastLeaveIsBig = false;
                }
                if(isLastLeaveIsBig)
                {
                    System.Threading.Thread.Sleep(2000);
                }
                int totalQueryCount = 0;
                ArrayList arListBarcodes = new ArrayList();
                ArrayList arListTmp = new ArrayList();
                RoadInfo roadInfoGen = new bean.RoadInfo();
                Hashtable htRoadMap = new Hashtable();
                bool isNewStart = false;
                string barcode = "";
                ArrayList arListBarCodes = new ArrayList();
                arListBarCodes.Clear();
                DateTime dtStart = System.DateTime.Now;
                DelayAnaHelperGen.Instance.m_arlist_Delayed_BarCodes.Clear();


                if (arListTmp.Count > 0)
                {
                    arListBarcodes.Add(arListTmp.Clone());
                    arListTmp.Clear();
                }
                int sumQueryTime = 0;
                int avgQueryTime = 2;
                int intProcess = 0;


                string httpRes = "";
                ArrayList arListAllGXTask = new ArrayList();
                int curCount = 0;
                ArrayList arListTmp3= (ArrayList)arListDepBarcodes;
                StoParams.Instance.totalParcels = arListTmp3.Count;
                foreach (ParcelBean parcelBean in (ArrayList)arListDepBarcodes)
                {
                    StoParams.Instance.curParces++;
                    if (StoParams.Instance.totalParcels>20)
                    {
                        System.Threading.Thread.Sleep(2000);                        
                    }
                    if (curCount > 0 && curCount % 10 == 0)
                    {
                        System.Threading.Thread.Sleep(3000);
                    }
                    curCount++;
                    string strBarCode = parcelBean.wayBill;
                    StoParams.Instance.currentBarcode = strBarCode;
                    string pickName = "";
                    string orderSource = "";
                    
                    string curSiteToken = "";
                    string customerName = "";
                    if (!UtilityHelp.Instance.resetToken(strBarCode,out pickName,out orderSource,out curSiteToken,out customerName))
                    {
                        if (curSiteToken == "")
                        {
                            StoParams.Instance.htResWechat[strBarCode] = " 发件网点 " + pickName + " 登录失效";
                            continue;
                        }
                        StoParams.Instance.htResWechat[strBarCode] = " 发件网点 " + pickName + " 非我司单号  无法处理";
                        continue;
                    }
                    if(pickName.Contains("授权"))
                    {
                        continue;
                    }
                    parcelBean.customerName = customerName;
                    UtilityHelp.Instance.resetWechat(parcelBean, pickName);
                    string wechatName = "";
                    StoParams.Instance.resMsg = "";
                    intProcess++;
                    string strLeftTime = "10";

                    bool isReturnInRepley = false;
                    ArrayList arListRoadMap = JTDelayHelper.Instance.getRoadMap(strBarCode, out httpRes, arListAllGXTask);
                    ArrayList arListDBOrJB = new ArrayList();
                    
                    if (!httpRes.Contains("成功"))
                    {
                        StoParams.Instance.resMsg = httpRes;
                        if(QueryMessageRes!=null)
                        {
                            QueryMessageRes();
                        }
                        
                        if(isManual)
                        {
                            System.Threading.Thread.Sleep(200);
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(150);
                        }
                        
                        resMsg = "请求失败";
                        StoParams.Instance.htResWechat[strBarCode] = "请求失败";
                        continue;
                    }
                    if (arListRoadMap.Count == 0 && httpRes.Contains("成功"))
                    {
                        if (isManual)
                        {
                            System.Threading.Thread.Sleep(300);
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(150);
                        }
                        
                        StoParams.Instance.htResWechat[strBarCode] = "快件未发出";
                        continue;
                    }
                    if(JTDelayHelper.Instance.isOnlySelfSiteMap(arListRoadMap))
                    {
                        if (isManual)
                        {
                            System.Threading.Thread.Sleep(300);
                        }
                        else
                        {
                            System.Threading.Thread.Sleep(300);
                        }
                        StoParams.Instance.htResWechat[strBarCode] = "只有本网点路由";
                        continue;
                    }

                    if (JTDelayHelper.Instance.isNoSiteToLeaveMsg(arListRoadMap,pickName))
                    {
                        System.Threading.Thread.Sleep(300);
                        StoParams.Instance.htResWechat[strBarCode] = "快件未到转运";
                        continue;
                    }
          
             
                    string returnMySite = "";
                    if (JTDelayHelper.Instance.isLastSiteIsMyself(arListRoadMap,out returnMySite))
                    {
                        StoParams.Instance.htResWechat[strBarCode] = returnMySite;
                        System.Threading.Thread.Sleep(300);
                        continue;
                    }
                    string commentType = "";
                    string strInterCeptSiteCode = "";



                    string siteName = "";
                    string leaveMsg = "";
                    bool hasSleep = false;
                    JTDelayHelper.Instance.parseManualBarcode(arListRoadMap, out commentType, out strInterCeptSiteCode, out siteName,out leaveMsg, parcelBean.isYishi,true);
                    leaveMsg = JTDelayHelper.Instance.getGuifanYishiMsg();
                    bool isLeaveSuccess = false;
                    if (strInterCeptSiteCode != "")
                    {
                        parcelBean.replyType = "规范拦截未退回";
                        parcelBean.isYishi = true;



                        string errorMsg = "";

                        string minuteDate = System.DateTime.Now.ToString("yyyy-MM-dd HH:mm");
                        int totalMinutes = 0;
                        if(!htCurMinutes.ContainsKey(minuteDate))
                        {
                            htCurMinutes[minuteDate] = 1;
                        }
                        else
                        {
                            totalMinutes = Convert.ToInt32(htCurMinutes[minuteDate]) + 1;
                            htCurMinutes[minuteDate] = totalMinutes;
                        }
                        if(totalMinutes>10)
                        {
                            System.Threading.Thread.Sleep(1000);

                        }

                        if (!JTDelayHelper.Instance.isParcelLeaveFirstCenter(arListRoadMap, pickName))
                        {
                            parcelBean.replyType = "催件未出集散或转运";
                            StoParams.Instance.htResWechat[strBarCode] = "快件未出" + "集散/转运" + "暂时无法留言";
                            continue;
                        }

                        
                        
                        
                        FrameDecoderManager.Instance.lastSendRes = System.DateTime.Now;
                        string fullMsg = "";
                        string phoneNumber = "";
                        if (parcelBean.phoneNumList.Count > 0)
                        {
                            phoneNumber = parcelBean.phoneNumList[parcelBean.phoneNumList.Count-1];
                        }

                        if (!parcelBean.isYishi)
                        {
                            UtilityHelp.Instance.parseIsYishi(parcelBean, arListRoadMap);
                        }
                        if (addComment(phoneNumber,strBarCode, commentType,parcelBean.ori_addr,leaveMsg, parcelBean.wechat, out errorMsg, out fullMsg, parcelBean.isYishi, parcelBean.roomID))
                        //if (UtilityHelp.Instance.addCommentReg(strBarCode, commentType, false))
                        {
                            StoParams.Instance.addCommentParcels++;
                            StoParams.Instance.curDelayBarcode = strBarCode;
                            StoParams.Instance.resMsg = "留言成功";
                            if (isManual)
                            {
                                if(GotDelayBarcode!=null)
                                {
                                    GotDelayBarcode();
                                }
                                
                            }
                                
                            StoParams.Instance.currentBarcode = strBarCode;
                            StoParams.Instance.resMsg = " 以上单号已联系网点催促";
                            if (isManual)
                            {
                                if(AddMessage!=null)
                                {
                                    AddMessage.Invoke();
                                }
                                
                            }

                            /*if (siteName.Contains("网点") && !commentType.Contains("签收") && fullMsg.Length > 10)
                            {
                                fullMsg = UtilityHelp.Instance.getUsefullContent(fullMsg, siteName);
                                hasSleep = true;
                                Thread.Sleep(200);
                                signProblemParcel(strBarCode, out errorMsg, strInterCeptSiteCode, siteName, fullMsg);
                            }*/

                            isLeaveSuccess = true;
                        }
                        else
                        {
                            StoParams.Instance.curFailedBarcode = strBarCode;
                            StoParams.Instance.resMsg = "留言失败";
                            if (isManual)
                            {
                                if(GotFailedBarcode!=null)
                                {
                                    GotFailedBarcode();
                                }
                                
                            }
                                

                            StoParams.Instance.curFailedBarcode = strBarCode;
                            StoParams.Instance.resMsg = " 以上单号催促失败" + errorMsg;
                        }
                    }
                    resMsg = StoParams.Instance.resMsg;
                    bool isYiqingDelay = false;
                    string reswechatMsg = UtilityHelp.Instance.getLeaveMsgResponse(arListRoadMap, commentType, parcelBean, isYiqingDelay);
                    if(!StoParams.Instance.resMsg.Contains("失败"))
                    {
                        StoParams.Instance.htResWechat[strBarCode] = reswechatMsg;
                    }
                    else
                    {
                        StoParams.Instance.htResWechat[strBarCode] = resMsg;
                    }
                    
                    StoParams.Instance.resMsg = strBarCode + " " + StoParams.Instance.resMsg;
                    if(!isLeaveSuccess)
                    {
                        if(QueryMessageRes!=null)
                        {
                            QueryMessageRes.Invoke();
                        }
                        
                    }
                    
                    Random ran = new Random();

                    DoneHttpResponse();
                    int sleepTime = ran.Next(150, 300);
                    if(StoParams.Instance.totalParcels<20)
                    {
                        if (curCount > 8)
                        {
                            sleepTime = ran.Next(500, 800);
                            System.Threading.Thread.Sleep(sleepTime);
                        }
                        else
                        {
                            if(!hasSleep)
                            {
                                Thread.Sleep(sleepTime);
                            }
                            
                        }
                    }
                    
                    
                    StoParams.Instance.currentBarcode = strBarCode.Trim();

                  


                }



            }
            catch (Exception ex)
            {
                

            }
            int tiot = queryServerResTime;
           


        }


        public void parseCuijianBarcodes(object arListDepBarcodes)
        {
            
            parseLeaveMsgBoard(arListDepBarcodes);
        }

        public bool signProblemParcel(string strCurBarcode, out string msg, string strInterCeptSiteCode, string siteName, string fullLeaveMsg)
        {
            if(fullLeaveMsg.Length>100)
            {
                fullLeaveMsg = fullLeaveMsg.Substring(0, 100);
            }
           
            msg = "";
            bool res = false;
            try
            {
                /*string strQueryUrl = "https://jmsgw.jtexpress.com.cn/servicequality/problemPiece/queryOperationType?waybillNo=" + strCurBarcode;
                JObject jObj = new JObject();
                JArray jarry = new JArray();

                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string resMsg = HttpClient.HttpUtil.Instance.QueryCommon(strQueryUrl, postData, "GET", "batchProblem");
                if (!resMsg.Contains("请求成功"))
                {
                    msg = "登记失败";
                    return false;
                }

                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(resMsg);
                JArray records = (JArray)jsonObjRes["data"];
                if (records.Count == 0)
                {
                    msg = "登记失败";
                    return false;
                }
                string codeStr = "";
                for (int i = 0; i < records.Count; i++)
                {
                    JObject jsonObjFirst = (JObject)records[0];
                    JToken codeToken = jsonObjFirst["code"];
                    codeStr = codeToken.ToString();
                    if (codeStr == "4")
                    {
                        break;
                    }
                }
                codeStr = "1";

                strQueryUrl = "https://jmsgw.jtexpress.com.cn/basicdata/sysAbnormalPiece/select?size=999999&levelType=1&operatorType=" + codeStr;
                jObj = new JObject();
                jarry = new JArray();

                postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                resMsg = HttpClient.HttpUtil.Instance.QueryCommon(strQueryUrl, postData, "GET", "batchProblem");
                if (!resMsg.Contains("请求成功"))
                {
                    msg = "登记失败";
                    return false;
                }

                jsonObjRes = (JObject)JsonConvert.DeserializeObject(resMsg);
                JObject dataObj = (JObject)jsonObjRes["data"];
                records = (JArray)dataObj["records"];
                if (records.Count == 0)
                {
                    msg = "登记失败";
                    return false;
                }
                string assingType = "退回件";*/




                //string strUrl = "https://jmsgw.jtexpress.com.cn/basicdata/network/select/all?current=1&size=100&name=" + strInterCeptSiteCode + "&typeIds%5B0%5D=336&queryLevel=3";
                string strUrl = "https://jmsgw.jtexpress.com.cn/servicequality/problemPiece/getPageByName?current=1&size=10&queryLevel=3&name=" + siteName;
                if(StoParams.Instance.isRabbitWin)
                {
                    strUrl = "https://wdgw.jtexpress.com.cn/servicequality/problemPiece/getPageByName?current=1&size=10&queryLevel=3&name=" + siteName;
                }

                JObject jObj = new JObject();
                JArray jarry = new JArray();

                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string resMsg = HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData, "GET", "batchProblem");
                if (!resMsg.Contains("请求成功"))
                {
                    msg = "登记失败";
                    return false;
                }
                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(resMsg);
                JObject jsonObjData = (JObject)jsonObjRes["data"];
                JArray records = (JArray)jsonObjData["records"];
                if (records.Count == 0)
                {
                    msg = "网点编码：" + strInterCeptSiteCode + " 不存在,跳过登记环节";
                    return false;
                }
                string signStieCode = "";
                int receiveNetworkId = 0;
                for (int i = 0; i < records.Count; i++)
                {
                    JObject json = (JObject)records[i];
                    JToken code = json["code"];
                    JToken id = json["id"];
                    if (code != null)
                    {
                        if (code.ToString() == strInterCeptSiteCode || id.ToString() == strInterCeptSiteCode)
                        {
                            signStieCode = code.ToString();
                            if (id != null)
                            {
                                receiveNetworkId = Convert.ToInt32(id);
                            }
                            break;
                        }
                    }
                }
                if (signStieCode == "" || receiveNetworkId == 0)
                {
                    msg = "网点编码：" + strInterCeptSiteCode + " 不存在,跳过登记环节";
                    return false;
                }

                logHelp.LogHelper.Instance.writeLog("开始登记问题件:"+ strCurBarcode);

                string firstLevelTypeName = "催件";
                int probleTypeSubjectId = 100123;
                int probleTypeSubjectId2 = 100124;
                string problemTypeSubjectCode = "50";
                string secondLevelTypeCode = "50a";
                int secondLevelTypeId = 100124;
                string secondLevelTypeName = "催件a";
                bool resSign = signType(out msg, secondLevelTypeName, probleTypeSubjectId, probleTypeSubjectId2,
                                        problemTypeSubjectCode, secondLevelTypeCode, secondLevelTypeId, receiveNetworkId, strCurBarcode, fullLeaveMsg);
                if (resSign)
                {
                    return true;
                }

                return resSign;




            }
            catch (Exception ex)
            {

            }



            return res;
        }


        private bool signType(out string msg, string secondLevelTypeName, int probleTypeSubjectId, int probleTypeSubjectId2,
                                        string problemTypeSubjectCode, string secondLevelTypeCode, int secondLevelTypeId, int receiveNetworkId, string strCurBarcode
            , string fullLeaveMsg)
        {
            msg = "";
            bool res = false;
            try
            {
                string strUrl = "https://jmsgw.jtexpress.com.cn/servicequality/problemPiece/registration";
                if(StoParams.Instance.isRabbitWin)
                {
                    strUrl = "https://wdgw.jtexpress.com.cn/servicequality/problemPiece/registration";
                }
                string routerName = "batchProblem";
                JObject jObj = new JObject();
                jObj["countryId"] = "1";
                jObj["defaultKnow"] = null;
                jObj["firstLevelTypeName"] = secondLevelTypeName;
                jObj["knowNetwork"] = "";
                jObj["paths"] = "";
                jObj["probleTypeId"] = 1;
                jObj["probleTypeSubjectId"] = probleTypeSubjectId;
                jObj["probleTypeSubjectId2"] = probleTypeSubjectId2;
                jObj["problemPieceId"] = "";
                jObj["problemTypeSubjectCode"] = problemTypeSubjectCode;
                jObj["receiveNetworkId"] = receiveNetworkId;
                jObj["replyContent"] = "";
                //jObj["probleDescription"] = "此件麻烦尽快退回，客户已拒收，商家急催，避免客户投诉，如已遗失，麻烦联系我司处理理赔 ";
                jObj["probleDescription"] = fullLeaveMsg;
                jObj["replyContentImg"] = new JArray();
                jObj["replyStatus"] = 0;
                jObj["secondLevelTypeCode"] = secondLevelTypeCode;
                jObj["secondLevelTypeId"] = probleTypeSubjectId2;
                jObj["secondLevelTypeName"] = secondLevelTypeName;
                jObj["uploadDataProp"] = "";
                jObj["waybillNo"] = strCurBarcode;
                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);

                string resMsg = HttpClient.HttpUtil.Instance.PostCommon(strUrl, postData, routerName);
                if (!resMsg.Contains("请求成功"))
                {
                    logHelp.LogHelper.Instance.writeLog("登记问题件失败:" + resMsg);
                    JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(resMsg);
                    JToken msgToke = jsonObjRes["msg"];

                    msg = "登记失败:" + msgToke.ToString();
                    return false;
                }
                else
                {
                    res = true;
                    msg = "登记成功";
                    return true;
                }
            }
            catch (Exception ex)
            {
                logHelp.LogHelper.Instance.writeLog("登记问题件异常:" + ex.ToString());

            }
            return res;
        }




        public bool addComment( string recvPhone, string strBarCode, string commentType, string addr, string leaveMsg,
            string wechatName, out string errorMsg, out string fullMsg,bool isYishi = false,string roomid="")
        {
            bool ret = false;
            errorMsg = "";
            fullMsg = leaveMsg;
            try
            {
                string origPhone = recvPhone;

                if (!utility.Utility.canLeaveMsgToday(strBarCode))
                {
                    LogHelper.Instance.writeLog(commentType + " " + strBarCode + " 不能留言，留言个数大于10");
                    return false;
                }

                if (recvPhone.Contains("-"))
                {
                    recvPhone = recvPhone.Substring(0, recvPhone.IndexOf('-'));
                }
                //string strUrl = "https://jmsgw.jtexpress.com.cn/servicequality/leaveMessage/getReceiveNetwork?waybillNo=" + strBarCode.Trim();
                string strUrl = "";
                JObject jObj = new JObject();
                JArray jarry = new JArray();
                jarry.Add(strBarCode);

                string postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                string res = "";
        

                string msg = "";
                ArrayList arListRoadMap = new ArrayList();
                ArrayList arListRoadGX = new ArrayList();

                arListRoadMap = JTDelayHelper.Instance.getRoadMap(strBarCode, out msg, arListRoadGX);
                SiteBean siteBean = UtilityHelp.Instance.getFirstSite(res, arListRoadMap);


                //SiteBean siteBean = UtilityHelp.Instance.getLastSite(res);
                if (siteBean.siteCode == "")
                {
                    if(!commentType.Contains("破损"))
                    {
                        string problemMsg = "";
                        if (!UtilityHelp.Instance.isLastRoadMapIsProblem(arListRoadMap, siteBean,out problemMsg))
                        {
                            siteBean = UtilityHelp.Instance.getBeanSite(arListRoadMap);
                        }                      
                        
                    }
                    else
                    {
                        siteBean = UtilityHelp.Instance.getBeanSiteDirtyParcel(arListRoadMap);
                    }                    
                    
                }
                if (siteBean.siteCode == "" && siteBean.siteId == "")
                {
                    if(siteBean.siteName==StoParams.Instance.register_site)
                    {
                        errorMsg = "忽略自身网点";
                    }
                    return ret;
                }

                if (siteBean.siteCode == "" && siteBean.siteId != "")
                {
                    try
                    {
                        strUrl = "https://jmsgw.jtexpress.com.cn/operatingplatform/networkInfo/getNetworkDetailInfoById?networkId=" + siteBean.siteId.Trim();
                        if(StoParams.Instance.isRabbitWin)
                        {
                            strUrl = "https://wdgw.jtexpress.com.cn/ops/wdbutler/networkInfo/getNetworkDetailInfoById?networkId=" + siteBean.siteId.Trim();
                        }
                        jObj = new JObject();
                        jarry = new JArray();
                        jarry.Add(strBarCode);

                        postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);
                        res = HttpClient.HttpUtil.Instance.QueryCommon(strUrl, postData, "GET", "trackingExpress");
                        if (res.Contains("请求成功"))
                        {
                            if (res.Contains("data"))
                            {
                                JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(res);

                                JObject jsonObjData = (JObject)jsonObjRes["data"]; ;
                                JToken networkCode = jsonObjData["networkCode"];
                                siteBean.siteCode = networkCode.ToString();

                            }

                        }
                    }
                    catch (Exception ex)
                    {

                    }

                }

                if (StoParams.Instance.register_site.Contains(siteBean.siteName.Trim())
                    || siteBean.siteName.Trim().Contains(StoParams.Instance.register_site))
                {
                    if(AddMessage!=null)
                    {
                        AddMessage.Invoke();
                    }
                    
                    return ret;
                }
                jObj["waybillNo"] = strBarCode.Trim();
                jObj["receiveNetworkId"] = Convert.ToInt32(siteBean.siteId.Trim());
                jObj["fileUrl"] = "";
                jObj["receiveNetworkName"] = siteBean.siteName.Trim();
                jObj["receiveNetworkCode"] = siteBean.siteCode.Trim();
                jObj["countryId"] = "1";
                jObj["leaveMessageType"] = 4;
                jObj["receiverPhone"] = recvPhone;


                SecondTypeBean secondTypeBean = new SecondTypeBean();


                /*if (wechatName == ""|| wechatName.Contains("wechat"))
                {
                    wechatName = MemberInfo.Instance.templeteAssign;
                }*/


                addr = addr.Replace("规范拦截未退回", "");
                addr = addr.Replace("拦截未退回", "");
                string tmplete = leaveMsg+" "+addr.Replace("规范拦截未退回","");

                if(tmplete.Contains(origPhone)&& origPhone.Length>7)
                {
                    if(StoParams.Instance.register_site.Contains("曹三")|| StoParams.Instance.register_site.Contains("菊城"))
                    {
                        if(!commentType.Contains("改地址"))
                        {
                            tmplete = tmplete.Substring(0, tmplete.IndexOf(origPhone) + origPhone.Length);
                        }
                        
                    }
                    
                }
                if(wechatName.Length>3)
                {
                    tmplete= tmplete+ " 协商加微信 " + wechatName;
                }

                if (commentType.Contains("破损"))
                {
                    DestroyTypeBean desBean = new DestroyTypeBean();
                    utility.Utility.getDestoryType(addr, desBean, commentType);

                    jObj["leaveMessageType"] = 1;
                    jObj["secondTypeName"] = desBean.name;
                    jObj["secondTypeCode"] = desBean.code;                   

                    tmplete = " 破损仲裁查件, 此件贵司到件破损，核实外包装破损，"+ desBean.name + " 客户信息 " + addr.Replace("客户信息", "") + " 协商加微信 " + wechatName;
                }

                if (commentType.Contains("延误查件"))
                {
                    tmplete = " 延误仲裁查件：此件到达网点后未及时配送，核实客户未收到，请帮忙尽快派送。 " + addr.Replace("延误查件","").Replace("延误仲裁查件","") + " 协商加微信 " + wechatName;
                    jObj["leaveMessageType"] = 2;
                    if (recvPhone != "")
                    {
                        UtilityHelp.Instance.parseSecondTypeYishiAndDelay(arListRoadMap, "延误仲裁", secondTypeBean, StoParams.Instance.register_site);
                        if (secondTypeBean.code != "")
                        {
                            jObj["secondTypeName"] = secondTypeBean.name;
                            jObj["secondTypeCode"] = secondTypeBean.code;
                            //jObj["leaveMessageType"] = 2;
                        }
                    }
                }

                if (leaveMsg=="")
                {
                    if (commentType.Contains("中转"))
                    {
                        tmplete =  addr+ " 协商加微信 " + wechatName;
                    }
                    else if (commentType.Contains("派件"))
                    {
                        tmplete = addr + " 协商加微信 " + wechatName;
                    }
                    else if (commentType.Contains("签收"))
                    {
                        tmplete = addr + " 协商加微信 " + wechatName;
                    }
                    else if (commentType.Contains("拦截"))
                    {
                        //tmplete = "此件已退款，商家急催要求尽快退回";
                        tmplete = addr + " 协商加微信 " + wechatName;
                    }
                    else if (commentType.Contains("改地址")|| addr.Contains("改地址"))
                    {
                        jObj["leaveMessageType"] = 5;
                        //tmplete = "此件已退款，商家急催要求尽快退回";
                        //tmplete = "延误仲裁查件：此件要求改地址转出，新地址:" + addr.Replace("改地址", "");
                        tmplete = "延误仲裁查件：此件要求改地址转出，新地址:" + addr.Replace("改地址", "");

                        if (MemberInfo.Instance.templeteReturn!="")
                        {
                            tmplete= tmplete + " 协商加微信 " + wechatName;
                        }
                        
                    }
                }
                else
                {
                    if(recvPhone != "")
                    {
                        if(!isYishi)
                        {
                            UtilityHelp.Instance.parseSecondTypeYishiAndDelay(arListRoadMap, "延误仲裁", secondTypeBean, StoParams.Instance.register_site);
                            if (secondTypeBean.code != "")
                            {
                                jObj["secondTypeName"] = secondTypeBean.name;
                                jObj["secondTypeCode"] = secondTypeBean.code;
                                jObj["leaveMessageType"] = 2;
                            }
                        }
                        else
                        {
                            UtilityHelp.Instance.parseSecondTypeYishiAndDelay(arListRoadMap, "遗失仲裁", secondTypeBean, StoParams.Instance.register_site);
                            if (secondTypeBean.code != "")
                            {
                                jObj["secondTypeName"] = secondTypeBean.name;
                                jObj["secondTypeCode"] = secondTypeBean.code;
                                jObj["leaveMessageType"] = 3;
                            }
                        }
                        
                    }
                }

                if(isYishi)
                {
                    UtilityHelp.Instance.parseSecondTypeYishiAndDelay(arListRoadMap, "遗失仲裁", secondTypeBean, StoParams.Instance.register_site);
                    if (secondTypeBean.code != "")
                    {
                        jObj["secondTypeName"] = secondTypeBean.name;
                        jObj["secondTypeCode"] = secondTypeBean.code;
                        jObj["leaveMessageType"] = 3;
                    }
                }
                if (commentType.Contains("改电话"))
                {
                    //jObj["leaveMessageType"] = 4;
                    //tmplete = "此件已退款，商家急催要求尽快退回";
                    tmplete = addr;
                    if (wechatName!="")
                    {
                        tmplete= tmplete+ " 协商加微信" + wechatName;
                    }
                    
                }
                if (commentType.Contains("破损"))
                {
                    jObj["leaveMessageType"] = 1;
                    //tmplete = "此件已退款，商家急催要求尽快退回";
                    //string destroyType = utility.Utility.getDestoryType(addr);
                    DestroyTypeBean desBean = new DestroyTypeBean();
                    utility.Utility.getDestoryType(addr, desBean, commentType);
                    jObj["secondTypeName"] = desBean.name;
                    jObj["secondTypeCode"] = desBean.code;

                    tmplete = " 破损仲裁查件, 此件贵司到件破损，核实外包装破损"+ desBean.name + " 客户信息:" + addr.Replace("客户信息","");

                    if (wechatName != "")
                    {
                        tmplete = tmplete + " 协商加微信" + wechatName;
                    }
                }
                if (commentType.Contains("改地址")|| addr.Contains("改地址"))
                {
                    jObj["leaveMessageType"] = 5;
                    tmplete = "延误仲裁查件,此件要求改地址转出，新地址:" + addr.Replace("改地址", "");
                    if (wechatName != "")
                    {
                        tmplete = tmplete + " 协商加微信" + wechatName;
                    }
                    //tmplete = " 改地址 " + addr.Replace("改地址", "")+ "协商加微信"+ wechatName;
                }
                if (isYishi)
                {
                    jObj["leaveMessageType"] = 3;
                }

                if(jObj["leaveMessageType"].ToString()=="3")
                {
                    tmplete = tmplete.Replace("延误仲裁", "遗失仲裁");
                    if(!tmplete.Contains("遗失仲裁"))
                    {
                        tmplete = "遗失仲裁查件 " + tmplete;
                    }
                    
                    UtilityHelp.Instance.parseSecondTypeYishiAndDelay(arListRoadMap, "遗失仲裁", secondTypeBean, StoParams.Instance.register_site);
                    if(secondTypeBean.code!="")
                    {
                        jObj["secondTypeName"] = secondTypeBean.name;
                        jObj["secondTypeCode"] = secondTypeBean.code;
                    }

                }
                /*if (jObj["leaveMessageType"].ToString() == "1")
                {
                    tmplete = tmplete.Replace("延误仲裁", "破损仲裁");
                }*/
                if(StoParams.Instance.register_site.Contains("阳江"))
                {
                    jObj["leaveMessageType"] = 3;
                    tmplete = tmplete.Replace("协商加微信", "遗失理赔+");
                    if (jObj["leaveMessageType"].ToString() != "3"&& jObj["leaveMessageType"].ToString() != "1")
                    {
                        tmplete = tmplete.Replace("延误仲裁", "遗失仲裁");
                        if (!tmplete.Contains("遗失仲裁"))
                        {
                            tmplete = "遗失仲裁查件 " + tmplete;
                        }
                        UtilityHelp.Instance.parseSecondTypeYishiAndDelay(arListRoadMap, "遗失仲裁", secondTypeBean, StoParams.Instance.register_site);
                        if (secondTypeBean.code != "")
                        {
                            jObj["secondTypeName"] = secondTypeBean.name;
                            jObj["secondTypeCode"] = secondTypeBean.code;
                        }
                    }

                }

                /*if(roomid!=""&& roomid.Contains("45090593079@chatroom")&& !addr.Contains("改地址"))
                {
                    tmplete = tmplete + " 该件为兔优达生鲜特殊件，请务必电联，加急派送，请勿私自入库。";
                }*/



                tmplete = tmplete.Replace("，", ",");
                tmplete = tmplete.Replace("延误仲裁查件", "");
                tmplete = tmplete.Replace("遗失仲裁查件", "");
                tmplete = tmplete.Replace("破损仲裁查件", "");
                tmplete = tmplete.Trim().TrimStart(',');

                /*if (commentType.Contains("中转"))
                {
                    tmplete = MemberInfo.Instance.templeteTransfer;
                }
                else if (commentType.Contains("派件"))
                {
                    tmplete = MemberInfo.Instance.templeteAssign;
                }
                else if (commentType.Contains("签收"))
                {
                    tmplete = MemberInfo.Instance.templeteQS;
                }
                else if (commentType.Contains("拦截"))
                {
                    //tmplete = "此件已退款，商家急催要求尽快退回";
                    tmplete = MemberInfo.Instance.templeteReturn;
                }*/
                jObj["replyContent"] = tmplete;

                fullMsg = tmplete;

                /*if(tmplete.Length<100)
                {
                    fullMsg = tmplete;
                }
                else
                {
                    if(tmplete.Contains(","))
                    {
                        string priFix = tmplete.Substring(0, tmplete.IndexOf(','));
                        fullMsg = priFix + " " + addr;
                    }
                    
                }
                if (fullMsg.Length < 2)
                {
                    fullMsg = addr;
                }
                if (fullMsg.Length>100)
                {
                    fullMsg = fullMsg.Substring(0, 100);
                }*/



                string postUrl = "https://jmsgw.jtexpress.com.cn/operatingplatform/messageBoard/add";

                //string postUrl = "https://jmsgw.jtexpress.com.cn/servicequality/leaveMessage/add";
                if(StoParams.Instance.isRabbitWin)
                {
                    postUrl = "https://wdgw.jtexpress.com.cn/ops/wdbutler/messageBoard/add";
                }
                postData = Newtonsoft.Json.JsonConvert.SerializeObject(jObj);

          
                //return true;
          
                string messge = HttpClient.HttpUtil.Instance.addLeaveMessage(postUrl, postData);
                if (messge.Contains("成功"))
                {
                    ret = true;
                    StoParams.Instance.resMsg = "留言成功";
                    UtilityHelp.Instance.setTokenValid(MemberInfo.Instance.token);
                }
                else
                {
                    logHelp.LogHelper.Instance.writeLog(messge);
                    StoParams.Instance.resMsg = "留言失败";
                    if(messge.Contains("用户请求接口次数达到限流告警"))
                    {
                        logHelp.LogHelper.Instance.writeLog(" 留言接口 用户请求接口次数达到限流告警" + strBarCode+" token="+ MemberInfo.Instance.token);
                        errorMsg = "用户请求接口次数达到限流告警";
                        UtilityHelp.Instance.setTokenInvalid(MemberInfo.Instance.token);
                    }
                    else
                    {
                        JObject jsonObjRes = (JObject)JsonConvert.DeserializeObject(messge);
                        errorMsg = jsonObjRes["msg"].ToString();
                    }
                }
                if(QueryMessageRes!=null)
                {
                    QueryMessageRes.Invoke();
                }
                
            }
            catch (Exception ex)
            {

            }
            return ret;
        }

        public void stopThread()
        {
            try
            {
                ArrayList arListT = new ArrayList();
                ResultBean resultBean = new ResultBean();
                if (fThreadDep != null && (fThreadDep.ThreadState == ThreadState.Running || fThreadDep.ThreadState == ThreadState.Background))
                {
                    /*if (totalQueryCount > 0)
                    {
                        resultBean.countTime = totalQueryCount;

                        updateQueryCount(resultBean);
                    }*/
                    fThreadDep.Abort();
                    if (swb != null)
                    {
                        swb.Close();
                    }

                }

    
            }
            catch (Exception ex)
            {

            }
        }


        private void initLog()
        {
            try
            {
                string dir = System.Environment.CurrentDirectory;
                bFile = new FileStream(dir + "\\error.txt", FileMode.Create);
                if (swb == null)
                {
                    swb = new StreamWriter(bFile);
                    swb.WriteLine("begin");

                }
            }
            catch (Exception ex)
            {

            }

        }


    }
}
